/** 
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2020-10-29 07:48:00 
 */

const cacheHelper = require('../../../../../helper/cache_helper.js');
const pageHelper = require('../../../../../helper/page_helper.js');
const AdminBiz = require('../../../../../comm/biz/admin_biz.js');
const PassportBiz = require('../../../../../comm/biz/passport_biz.js');
const constants = require('../../../../../comm/constants.js');
const RequestBiz = require('../../../../../comm/biz/request_biz.js');

Page({
	data: {
		user: null,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (PassportBiz.isLogin()) {
			let user = PassportBiz.getToken();
			this.setData({ user });

			let radarName = cacheHelper.get(constants.CACHE_RADAR);
			if (!radarName) {
				let reqData = await RequestBiz.request("/api/ysjl/user/getDict", {code: 'radar'});
				radarName = reqData.data.data;
				cacheHelper.set(constants.CACHE_RADAR, radarName, constants.CACHE_RADAR_EXPIRE);
			}
			
			let _self = this;
			wx.createSelectorQuery().select('#head').boundingClientRect(function (rect) {
			}).exec(function (res) {
				_self.drawRadar({
					numCount: 6,
					numSlot: 5,
					x: res[0].width / 2,
					y: res[0].height / 2,
					radarName: radarName,
				}, user.abilityList);
			});
		}


	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {  
		PassportBiz.loginSilenceMust(this); 
		this._loadUser();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	_loadUser: async function (e) {
		if (PassportBiz.isLogin()) {
			let user = PassportBiz.getToken();
			this.setData({ user });
		}

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () { 
		await this._loadUser();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},


	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () { },
	url: function (e) {
		pageHelper.url(e, this);
	},

	bindSetTap: function (e, skin) {
		let _self = this;
		let itemList = ['清除缓存'];
		if (this.data.user && this.data.user.manager === '1') {
			itemList = ['清除缓存', '后台管理'];
		}
		wx.showActionSheet({
			itemList,
			success: async res => {
				let idx = res.tapIndex;
				if (idx == 0) {
					_self.setData({
						user: null
					});
					cacheHelper.clear();
					pageHelper.showNoneToast('清除缓存成功');
				}

				if (idx == 1 && this.data.user && this.data.user.manager === '1') {
					AdminBiz.adminLogin(this, 'admin', 'admin@ysjl2022');
				}
			},
			fail: function (res) { }
		})
	},

	drawRadar:function (config, data) {
		let _self = this;

		wx.createSelectorQuery().select('#firstCanvas').context(function (res) {
			let context = null;
			context = res.context;
			config.mAngle = Math.PI * 2 / config.numCount;
			config.mRadius = 100;
			// 画边
			_self.drawEdge(config, context)
			// 绘制连接点
			_self.drawLinePoint(config, context)
			// 设置数据
			_self.drawRegion(config, context, data, 'rgb(136, 176, 248)')
			// 设置文本数据
			_self.drawTextCans(config, context)
			// 设置节点
			_self.drawCircle(config, context, data, 'rgb(136, 176, 248)')
			// 开始绘制
			context.draw()
		}).exec();
	},

	// 绘制6条边
  drawEdge: function(config, context){
    context.setStrokeStyle("skyblue")
    context.setLineWidth(2)  //设置线宽
		for (let i = 0; i < config.numSlot; i++) {
			//计算半径
			context.beginPath()
			let rdius = config.mRadius / config.numSlot * (i + 1)
			//画6条线段
			for (let j = 0; j < config.numCount; j++) {
				//坐标
				let x = config.x + rdius * Math.cos(config.mAngle * j);
				let y = config.y + rdius * Math.sin(config.mAngle * j);
        context.lineTo(x, y);
      }
      context.closePath()
      context.stroke()
    } 
	},
	
	// 绘制连接点
  drawLinePoint:function(config, context){
    context.beginPath();
		for (let k = 0; k < config.numCount; k++) {
			context.moveTo(config.x, config.y);
			let x = config.x + config.mRadius * Math.cos(config.mAngle * (k + 1));
			let y = config.y + config.mRadius * Math.sin(config.mAngle * (k + 1));
      context.lineTo(x, y);
    }
    context.stroke();
	},
	
 //绘制数据区域(数据和填充颜色)
	drawRegion: function (config, context, data, color){
		context.beginPath();
		for (let m = 0; m < config.numCount; m++){
			let x = config.x + config.mRadius * Math.cos(config.mAngle * m) * data[m] / 100;
			let y = config.y + config.mRadius * Math.sin(config.mAngle * m) * data[m] / 100;
			context.lineTo(x, y);
		}
		context.closePath();
		context.setFillStyle(color)
		context.fill();
	},
	
	//绘制文字
  drawTextCans: function (config, context){
		context.setFillStyle("skyblue")
		context.font = 'bold 17px cursive' //设置字体
		for (let n = 0; n < config.numCount; n++) {
			let x = config.x + config.mRadius * Math.cos(config.mAngle * n);
			let y = config.y + config.mRadius * Math.sin(config.mAngle * n);
			//通过不同的位置，调整文本的显示位置
			if (config.mAngle * n >= 0 && config.mAngle * n <= Math.PI / 2) {
				context.fillText(config.radarName[n], x + 5, y + 5);
			} else if (config.mAngle * n > Math.PI / 2 && config.mAngle * n <= Math.PI) {
				context.fillText(config.radarName[n], x - context.measureText(config.radarName[n]).width - 7, y + 5);
			} else if (config.mAngle * n > Math.PI && config.mAngle * n <= Math.PI * 3 / 2) {
				context.fillText(config.radarName[n], x - context.measureText(config.radarName[n]).width  -5, y);
			} else {
				context.fillText(config.radarName[n], x + 7, y + 2);
			}
		}
	},

	//画点
	drawCircle: function(config, context, data, color){
		let r = 3; //设置节点小圆点的半径
		for(let i = 0; i < config.numCount; i++){
			let x = config.x + config.mRadius * Math.cos(config.mAngle * i) * data[i] / 100;
			let y = config.y + config.mRadius * Math.sin(config.mAngle * i) * data[i] / 100;
			context.beginPath();
			context.arc(x, y, r, 0, Math.PI * 2);
			context.fillStyle = color;
			context.fill();
		}
	 },

})